/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import dao.DBManager;
//import font.Font2;
import java.util.Vector;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.IKeyListener;
import model.ErrorMessage;
import ui.Form;
import ui.RadioBoxItem;

/**
 *
 * @author kien.tran
 */
public class ConfigDataForm extends AForm implements ICommandListener, IKeyListener {

    private Form form;
    private String title;
    private Vector fonts;
    private Vector colors;
    private Vector menus;
    private String s;

    public ConfigDataForm() {
    }

    public ConfigDataForm(MStockMidlet control, Display display, int branch, int option) {
        super(control, display);
        this.branch = branch;
        this.option = option;
    }

    protected void initData() {
        lang = new String[]{"Chọn cỡ chữ", "Chọn giao diện", "Cập nhật dữ liệu", "Hiển thị màn hình chính"};

        recycle = control.getRecycle();
        icon = control.getIcons()[7];

        if (option == State.SIZE){
            title = lang[0];            
//            s = String.valueOf(control.getNormalFont().getFontSize());
            
            fonts = new Vector(3);
//            fonts.addElement(new RadioBoxItem(String.valueOf(Font2.SIZE10N), "Nhỏ"));
//            fonts.addElement(new RadioBoxItem(String.valueOf(Font2.SIZE12N), "Vừa"));
//            fonts.addElement(new RadioBoxItem(String.valueOf(Font2.SIZE14N), "Lớn"));

        } else if (option == State.COLOR){
            title = lang[1];
            s = String.valueOf(control.getStyle().getStyle());

            colors = new Vector(3);
            colors.addElement(new RadioBoxItem(String.valueOf(StyleManager.STYLE_PURPLE), "Màu tím"));
            colors.addElement(new RadioBoxItem(String.valueOf(StyleManager.STYLE_ORANGE), "Màu cam"));
            colors.addElement(new RadioBoxItem(String.valueOf(StyleManager.STYLE_GREEN), "Màu xanh"));            
        
        } else if (option == State.OTHER){
            title = lang[3];
            s = control.getMenuView();

            menus = new Vector(2);
            menus.addElement(new RadioBoxItem(MStockMidlet.DISPLAY_GRID, "Dạng biểu tượng"));
            menus.addElement(new RadioBoxItem(MStockMidlet.DISPLAY_LIST, "Dạng danh sách"));
        }
    }

    protected void initGui() {
        form = new Form(display, control, title, background, icon);
        ok = new Command("Chọn", Command.OK);
        back = new Command("Trở về", Command.BACK);
        form.setSelectCommand(ok);
        form.setCancelCommand(back);
        form.setCommandListener(this);
        form.setKeyListener(this);

        showData();
    }

    protected void loadData() {
    }

    protected void showData() {
        if (option == State.SIZE){
            RadioBoxItem radio;
            for (int i = 0; i < fonts.size(); i++){
                radio = (RadioBoxItem)fonts.elementAt(i);
                if (radio.getId().equals(s)){
                    radio.setChecked(true);
                }
                this.form.addItem(radio);
            }

        } else if (option == State.COLOR){
            RadioBoxItem radio;
            for (int i = 0; i < colors.size(); i++){
                radio = (RadioBoxItem)colors.elementAt(i);
                if (radio.getId().equals(s)){
                    radio.setChecked(true);
                }
                this.form.addItem(radio);
            }

        } else if (option == State.OTHER){
            RadioBoxItem radio;
            for (int i = 0; i < menus.size(); i++){
                radio = (RadioBoxItem)menus.elementAt(i);
                if (radio.getId().equals(s)){
                    radio.setChecked(true);
                }
                this.form.addItem(radio);
            }
        }

        this.form.show();
    }

    protected void showError(ErrorMessage error) {
    }

    public void commandAction(Command command) {
        if (command == ok) {
            int index = form.getSelectedIndex();

            if (option == State.SIZE){
                switch (index){
                    case 0:
//                        control.setFont(Font2.SIZE10N, Font2.SIZE10B);
//                        DBManager.updateSetting(DBManager.CONFIG, "FS", Font2.SIZE10N + ":" + Font2.SIZE10B);
                        control.back(1);
                        break;

                    case 1:
//                        control.setFont(Font2.SIZE12N, Font2.SIZE12B);
//                        DBManager.updateSetting(DBManager.CONFIG, "FS", Font2.SIZE12N + ":" + Font2.SIZE12B);
                        control.back(1);
                        break;

                    case 2:
//                        control.setFont(Font2.SIZE14N, Font2.SIZE14B);
//                        DBManager.updateSetting(DBManager.CONFIG, "FS", Font2.SIZE14N + ":" + Font2.SIZE14B);
                        control.back(1);
                        break;
                }

            } else if (option == State.COLOR){
                switch (index){
                    case 0:
                        control.setStyle(StyleManager.STYLE_PURPLE);
                        DBManager.update(DBManager.DB_CONFIG, "ST", String.valueOf(StyleManager.STYLE_PURPLE));
                        control.back(1);
                        break;

                    case 1:
                        control.setStyle(StyleManager.STYLE_ORANGE);
                        DBManager.update(DBManager.DB_CONFIG, "ST", String.valueOf(StyleManager.STYLE_ORANGE));
                        control.back(1);
                        break;

                    case 2:
                        control.setStyle(StyleManager.STYLE_GREEN);
                        DBManager.update(DBManager.DB_CONFIG, "ST", String.valueOf(StyleManager.STYLE_GREEN));
                        control.back(1);
                        break;
                }

            } else if (option == State.OTHER){
                switch (index){
                    case 0:
                        control.setMenuView(MStockMidlet.DISPLAY_GRID);
                        DBManager.update(DBManager.DB_CONFIG, "MV", MStockMidlet.DISPLAY_GRID);
                        control.back(1);
                        break;

                    case 1:
                        control.setMenuView(MStockMidlet.DISPLAY_LIST);
                        DBManager.update(DBManager.DB_CONFIG, "MV", MStockMidlet.DISPLAY_LIST);
                        control.back(1);
                        break;
                }
            }

        } else if (command == back) {
            control.back(0);
        }
    }

    public void keyAction(int key) {
        if (key == Form.FIRE) {
            commandAction(ok);
        }
    }
}
